package com.xpalive.helper;

import com.xpalive.pojo.DocAttr;
import com.xpalive.pojo.UrlAttr;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by xiongping on 2017/5/21.
 */
public class DocReader {

    private final static Logger LOGGER = LoggerFactory.getLogger(DocReader.class);


    private Document document;
    private String location;

    public DocAttr getByUrl(String fromUrlText,String fromUrl,String url){
        try {
            Connection connect = Jsoup.connect(url);
            this.document = connect.get();
            this.location = this.document.location();
            DocAttr docAttr = new DocAttr();
            docAttr.setFromUrlText(fromUrlText);
            docAttr.setFromUrl(fromUrl);
            docAttr.setLocation(this.location);
            docAttr.setRequestUrl(url);
            if(!docAttr.isNotFoundPage()){
                String body = "";/* this.document.html(); */
                if(!docAttr.isErrorPage(body)){
                    docAttr.setDocUrls(getDocumentUrl());
                    docAttr.setImageUrls(getImageUrl());
                    LOGGER.info("正常页面--跳转地址:{},跳转链接名称:《{}》,实际地址:{},《请求地址:{}》",docAttr.getFromUrl(),docAttr.getFromUrlText(),docAttr.getLocation(),docAttr.getRequestUrl());
                }else{
                    LOGGER.error("页面错误ErrorPage--跳转地址:{},跳转链接名称:《{}》,实际地址:{},请求地址:{}",docAttr.getFromUrl(),docAttr.getFromUrlText(),docAttr.getLocation(),docAttr.getRequestUrl());
                }
            }else{
                LOGGER.error("页面找不到NotFound--跳转地址:{},跳转链接名称:《{}》,实际地址:{},请求地址:{}",docAttr.getFromUrl(),docAttr.getFromUrlText(),docAttr.getLocation(),docAttr.getRequestUrl());
            }
            return docAttr;
        } catch (IOException | IllegalArgumentException e) {
            if(null == url || "".equals(url)){
                url = "空链接";
            }
            LOGGER.error("页面错误Exception--跳转地址:{},跳转链接名称:《{}》,实际地址:{}",fromUrl,fromUrlText,url);
        }
        return null;
    }

    private List<UrlAttr> getImageUrl() {
        final Elements links = this.document.getElementsByTag("img");
        UrlAttr urlAttr;
        List<UrlAttr> imageUrls = new ArrayList<>();
        for (Element element : links) {
            String src = element.attr("src");
            urlAttr = new UrlAttr();
            urlAttr.setHtml(element.html());
            urlAttr.setUrl(src);
            urlAttr.setFrom(this.location);
            imageUrls.add(urlAttr);
        }
        return imageUrls;
    }

    private List<UrlAttr> getDocumentUrl() {
        final Elements links = this.document.getElementsByTag("a");
        UrlAttr urlAttr;
        List<UrlAttr> docUrls = new ArrayList<>();
        for (Element element : links) {
            String href = element.absUrl("href");
            urlAttr = new UrlAttr();
            urlAttr.setHtml(element.html());
            urlAttr.setUrl(href);
            urlAttr.setFrom(this.location);
            docUrls.add(urlAttr);
        }
        return docUrls;
    }

}
